package net.roseboy.classfinal.plugin.dependency;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ArtifactResolver;
import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.artifact.filter.ScopeArtifactFilter;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.AbstractLogEnabled;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Tindy
 * @date 2023/2/22
 * @describe
 */
@Component( role = DependencyResolver.class )
public class DefaultDependencyResolver extends AbstractLogEnabled implements DependencyResolver{
    @Requirement
    private ArtifactResolver resolver;

    @Requirement
    private ArtifactMetadataSource metadataSource;

    public List<String> getExtLibPaths(MavenProject project, ArtifactRepository localRepository) throws ArtifactResolutionException, ArtifactNotFoundException {
        List<String> extLibPaths=new ArrayList<>();
        ScopeArtifactFilter scopeFilter = new ScopeArtifactFilter();
        scopeFilter.setIncludeCompileScope( true );
        scopeFilter.setIncludeRuntimeScope( true );
        ArtifactResolutionResult result = resolver.resolveTransitively(project.getDependencyArtifacts(), project.getArtifact(),
                project.getManagedVersionMap(), localRepository, project.getRemoteArtifactRepositories(),
                metadataSource, scopeFilter);
        for (Object o : result.getArtifacts()) {
            Artifact artifact = (Artifact) o;
            extLibPaths.add(artifact.getFile().getAbsolutePath());
        }
        return extLibPaths;
    }
}
